package com.meichuang.meipian.video.music;

import android.media.MediaPlayer;

/**
 * Created by ken on 17/11/12.
 */

public class MusicPlayUtil {
    MediaPlayer player;

    public  void play(MusicModel model){
//        distoryMusic();
        player = new MediaPlayer();
        try{
            player.setDataSource(model.audioFilePath);//为多媒体对象设置播放路径
            player.prepare();//准备播放
        }catch (Exception e){
            e.printStackTrace();
        }
        player.setLooping(model.isLoop);
        player.setVolume(model.audioVolume,model.audioVolume);
        player.start();//开始播放
        //setOnCompletionListener 当当前多媒体对象播放完成时发生的事件
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
//                next();//如果当前歌曲播放完毕,自动播放下一首.
            }
        });
    }
    public  void distoryMusic(){
        if(player != null){
            try{
                player.stop();
                player.release();
                player=null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public void pause(){
        player.pause();
    }
    public void setVolume(float volume){
        player.setVolume(volume,volume);
    }
}
